package com.wzq.service.impl;

import com.wzq.fegin.CodeServiceFeignClient;
import com.wzq.pojo.LagouToken;
import com.wzq.pojo.LagouUser;
import com.wzq.repositary.TokenRepositary;
import com.wzq.repositary.UserRepositary;
import com.wzq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

/**
 * @author wzq.Jolin
 * @company none
 * @create 2020-09-14 11:44
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepositary userRepositary;

    @Autowired
    private TokenRepositary tokenRepositary;

    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    @Override
    public boolean register(String email, String password, String code) {
        //1-校验code是否正确  0正确1错误2超时
        Integer codeResult = codeServiceFeignClient.validate(email, code);
        if("1".equals(codeResult)){
            System.out.println("验证码错误");
            return false;
        }
        if("2".equals(codeResult)){
            System.out.println("验证码超时");
            return false;
        }

        //2-判断是否已注册
        if(isRegistered(email)){
            return false;
        }

        //3-注册
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        userRepositary.save(lagouUser);

        return true;
    }

    /**
     * 邮箱是否已注册
     * @param email
     * @return
     */
    @Override
    public boolean isRegistered(String email) {
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        Example<LagouUser> example = Example.of(lagouUser);
        Optional <LagouUser> ont = userRepositary.findOne(example);

        if(ont.isPresent()){
            return true;
        }

        return false;
    }

    /**
     *  登录
     * @param email
     * @param password
     * @return
     */
    @Override
    public String login(String email, String password) {
        //1-校验邮箱密码，获取用户信息
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        Example<LagouUser> example = Example.of(lagouUser);
        Optional <LagouUser> one = userRepositary.findOne(example);
        if(!one.isPresent()){
            return "";
        }

        LagouUser user = one.get();

        //2-生成token并存储(这里不考虑作废原有token的业务了，重点不在这里)
        String token = UUID.randomUUID().toString();
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(token);
        tokenRepositary.save(lagouToken);

        return token;
    }

    /**
     * 用token获取邮箱
     * @param token
     * @return
     */
    @Override
    public String info(String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        Example<LagouToken> example = Example.of(lagouToken);
        Optional<LagouToken> one = tokenRepositary.findOne(example);
        if(!one.isPresent()){
            return "";
        }

        lagouToken = one.get();
        return lagouToken.getEmail();
    }
}
